<?

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.reviews.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.tree.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.user_auth.php";

	// See if user is logged in 
	$user_auth = new user_auth();
	$user_auth->user_auth_cp();




// FUNCTION // Show home page
function display_home(){

	$tree = new tree();	
	$hotels = new hotels();	
	$reviews = new reviews;
	
	
	if(!empty($_SESSION['review_search'])){
		$search_params['tree_id'] = $_SESSION['review_search']['tree_id'];
		$search_params['status'] = $_SESSION['review_search']['status'];
		$review_values = $reviews->get_reviews($search_params);
	}
	
	
?>

<h2>User Reviews</h2>

<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>

<h3>Review Search</h3>

<div class="border_box">

<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">

<input type="hidden" name="action" value="search" />

<table class="libra">
	<tr>
		<td width="100"><strong>Status</strong></td>
		<td>
		<select name="status">
			<option value="">All</option>
			<option value="a" <?= selected($search_params['status'],'a') ?>>Approved</option>
			<option value="d" <?= selected($search_params['status'],'b') ?>>Disapproved</option>
			<option value="p" <?= selected($search_params['status'],'c') ?>>Pending</option>
		</select>
		</td>
	</tr>
	<tr>
		<td><strong>Hotel Tree ID</strong></td>
		<td><input type="text" name="tree_id" value="<?= $search_params['tree_id'] ?>"></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" value="submit" class="form_button"></td>
	</tr>
</table>

</form>

</div>

<br>

<? 

if(!empty($_SESSION['review_search'])){

if($review_values){

?>

<table class="aqua">
	<tr>
		<th width="100">Date</td>
	  <th width="350">Hotel</td>
	  <th width="150">Reviewer</td>
	  <th width="150">Title</td>
	  <th width="100">Status</td>
	  <th width="100">Options</td>
  </tr>
<?
	foreach($review_values as $value){
	
	
	// Get review tree type
	//$tree_values = $tree->get_branch($value['tree_id']);
	
	// Get hotel details
	$params['tree_id'] = $value['tree_id'];
	$hotel_values = $tree->get_branch($params,3);
	$hotel_values = $hotel_values[0];
	
?>
		<tr>
		<td><?= date('d/m/Y', $value['review_date']) ?></td>
		<td ><?= $hotel_values['name']." (".$value['tree_id'].")"; ?></td>
		<td ><?= $value['reviewer_name'] ?></td>
		<td ><?= $value['title'] ?></td>
		<td ><?= $value['status'] ?></td>
		<td><a href='<?= $_SERVER['PHP_SELF'] ?>?action=view&review_id=<?= $value['review_id'] ?>'>View</a> | 	<a href='<?= $_SERVER['PHP_SELF'] ?>?action=delete&review_id=<?= $value['review_id'] ?>' onclick="return confirm('Do you really want to delete this review?')">Delete</a></td>

		<td align='center' ><? //$users->get_activity_status($value['user_status']); ?></td>
		</tr>
<?
		$i++;
	}
?>

</table>

<? }else{ ?>


<p>No reviews found</p>


<?

}


}




}
// END FUNCTION








// FUNCTION // View review 
function view_review($values=''){

$hotels = new hotels();
$tree = new tree();


?>

<h2>User Reviews</h2>




<h4>Review</h4>

<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>

<div class="border_box">



<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" >

	<input type="hidden" name="action" value="update" />
	<input type="hidden" name="tree_id" value="<?= $values['tree_id'] ?>" />
	<input type="hidden" name="review_id" value="<?= $values['review_id'] ?>" />
    
    
    
<fieldset><legend>Review details</legend></fieldset>

<table class="libra">
  <tr>
    <td width="200"><strong>Review ID</strong></td>
        <td><?= $values['review_id'] ?></td>
      </tr>
      <tr>
        <td><strong>Date of review</strong></td>
        <td><?= date('d/m/Y',$values['review_date']) ?></td>
      </tr>
      <tr>
        <td><strong>Hotel</strong></td>
        <td><?
		
		
	// Get hotel details
	$params['tree_id'] = $values['tree_id'];
	$hotel_values = $tree->get_branch($params,3);
	$hotel_values = $hotel_values[0];
	
	echo $hotel_values['name']." (".$values['tree_id'].")";
		
		
		?></td>
      </tr>
	 <tr> 
        <td><strong>Reviewer Name</strong></td>
        <td><input name="reviewer_name" type="text" value="<?= $values['reviewer_name'] ?>" /></td>
     </tr>
	 <tr> 
        <td><strong>Review Title</strong></td>
        <td><input name="review_title" type="text" value="<?= $values['review_title'] ?>"  size="60" /></td>
     </tr>
	  <tr> 
        <td><strong>Review</strong></td>
        <td><textarea name="review" cols="70" rows="8"><?= $values['review'] ?></textarea></td>
	</tr>
      <tr> 
        <td><strong>Value</strong></td>
        <td>

	<select name="value">
		<option value="1" <?= selected($values['value'],'1') ?>>1</option>
		<option value="2" <?= selected($values['value'],'2') ?>>2</option>
		<option value="3" <?= selected($values['value'],'3') ?>>3</option>
		<option value="4" <?= selected($values['value'],'4') ?>>4</option>
		<option value="5" <?= selected($values['value'],'5') ?>>5</option>
	</select></td>
      </tr>
      <tr> 
        <td><strong>Location</strong></td>
        <td>
		
	<select name="location">
		<option value="1" <?= selected($values['location'],'1') ?>>1</option>
		<option value="2" <?= selected($values['location'],'2') ?>>2</option>
		<option value="3" <?= selected($values['location'],'3') ?>>3</option>
		<option value="4" <?= selected($values['location'],'4') ?>>4</option>
		<option value="5" <?= selected($values['location'],'5') ?>>5</option>
	</select>		</td>
      </tr>
      <tr> 
        <td><strong>Facilities</strong></td>
        <td>
		
	<select name="facilities">
		<option value="1" <?= selected($values['facilities'],'1') ?>>1</option>
		<option value="2" <?= selected($values['facilities'],'2') ?>>2</option>
		<option value="3" <?= selected($values['facilities'],'3') ?>>3</option>
		<option value="4" <?= selected($values['facilities'],'4') ?>>4</option>
		<option value="5" <?= selected($values['facilities'],'5') ?>>5</option>
	</select>		</td>
      </tr>
      <tr> 
        <td><strong>Cleanliness</strong></td>
        <td>
		
	<select name="cleanliness">
		<option value="1" <?= selected($values['cleanliness'],'1') ?>>1</option>
		<option value="2" <?= selected($values['cleanliness'],'2') ?>>2</option>
		<option value="3" <?= selected($values['cleanliness'],'3') ?>>3</option>
		<option value="4" <?= selected($values['cleanliness'],'4') ?>>4</option>
		<option value="5" <?= selected($values['cleanliness'],'5') ?>>5</option>
	</select>		</td>
      </tr>
      <tr> 
        <td><strong>Overall</strong></td>
        <td>
		
	<select name="overall">
		<option value="1" <?= selected($values['overall'],'1') ?>>1</option>
		<option value="2" <?= selected($values['overall'],'2') ?>>2</option>
		<option value="3" <?= selected($values['overall'],'3') ?>>3</option>
		<option value="4" <?= selected($values['overall'],'4') ?>>4</option>
		<option value="5" <?= selected($values['overall'],'5') ?>>5</option>
	</select>		</td>
      </tr>
	  
      <tr> 
        <td><strong>Status</strong></td>
        <td>

	<select name="status">
		<option value="a" <?= selected($values['status'],'a') ?>>Approved</option>
		<option value="d" <?= selected($values['status'],'d') ?>>Disaproved</option>
		<option value="p" <?= selected($values['status'],'p') ?>>Pending</option>
	</select></td>
      </tr>

      <tr> 
        <td><strong>User IP</strong></td>
        <td><?= $values['user_ip'] ?></td>
      </tr>
	  </table> 
<br>

<center>
<input type="button" value="Cancel" onClick="history.back();" class="form_button" />
<input type="submit" value="submit" class="form_button" />
</center>


</form>

</div>



<?
}
// END FUNCTION 







    

	$template = new template();
	


switch ($_REQUEST['action']){
	default:
		$template->head_control();
		if(count($_POST)){
			$_SESSION['review_search']['tree_id'] = $_POST['tree_id'];
			$_SESSION['review_search']['status'] = $_POST['status'];
		}
		display_home();
		$template->foot_control();
	break;
	case "view":
		$reviews = new reviews;
		$params['review_id'] = $_REQUEST['review_id'];
		$review_values = $reviews->get_review($params);
		$template->head_control();
		view_review($review_values);
		$template->foot_control();
	break;
	case "update":
		$reviews = new reviews;
		$review_values['tree_id'] = $_POST['tree_id'];
		$review_values['review_id'] = $_POST['review_id'];
		$review_values['reviewer_name'] = $_POST['reviewer_name'];
		$review_values['review_title'] = $_POST['review_title'];
		$review_values['review'] = $_POST['review'];
		$review_values['facilities'] = $_POST['facilities'];
		$review_values['value'] = $_POST['value'];
		$review_values['cleanliness'] = $_POST['cleanliness'];
		$review_values['location'] = $_POST['location'];
		$review_values['overall'] = $_POST['overall'];
		$review_values['status'] = $_POST['status'];
		
		$template->head_control();
		if($errors = $reviews->validate_review_form_admin($review_values)){
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "Your form had the following errors";
			$my_message['list'] = $errors;
			$GLOBALS['note'] = $my_message;
			view_review($review_values);
		}else{
			$reviews->update_review($review_values);
			$my_message = array();
			$my_message['type'] = 'pos';
			$my_message['message'] = "Review updated succesfully";
			$GLOBALS['note'] = $my_message;
			$params['review_id'] = $_REQUEST['review_id'];
			$review_values = $reviews->get_review($params);
			view_review($review_values);
		}
		$template->foot_control();
	break;
	case "delete";
		$reviews = new reviews;
		$reviews->delete_review($_REQUEST['review_id']);
		header("Location: reviews.php?action=deleted");
	break;
	
	case "deleted";
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "Review succesfully deleted.";
		$GLOBALS['note'] = $my_message;
		$template->head_control();
		display_home();
		$template->foot_control();
	break;
}
	



?>